<?php
// 获取订单数据
require_once $this->website['path']['action'] . '/app/token.php';

$uid = $basic['uid'];
$json = array('status' => 'error', 'msg' => '', 'data' => []);

$str = fn_post('orderid');

// 所有订单id
$orderid = json_decode($str, 1);
// 订单个数   判断是否是多个订单合并付款
$num = count($orderid);
// 支付总价格
$totals = 0;
// 订单id
$orderCode = 0;
// 支付商品名称
$title = '';

$productTitle = function ($id) use ($website) {
	if (!$id) {return false;}
	$arr = $website['class']['db']
		->table('product')
		->field('tit')
		->where('id=? and shutdown=0', $id)
		->one();
	if (!($arr['tit'] ?? false)) {return false;}
	return $arr['tit'];
};

if ($num === 1) {
	// 支付一个订单
	$order = $this->website['class']['db']
		->table('product_order')
		->field('total', 'postage', 'orderid', 'params')
		->where('id=? and uid=? and sta= 0', $orderid[0], $uid)
		->one();
	if (!($order['total'] ?? false)) {
		$json['msg'] = '未找到订单或已支付过,请刷新!';
		$json['code'] = 1001;
		$this->json_encode_encrypt($json);
	}
	$totals = $order['total'] + $order['postage'];
	$orderCode = $order['orderid'];
	$params = json_decode($order['params'], 1);
	$pid = $params[0]['pid'];
	$title = $productTitle($pid);
	if (!$title) {
		$json['msg'] = '有商品已下架或已被删除!';
		$json['code'] = 1004;
		$this->json_encode_encrypt($json);
	}
	if (count($params) > 1) {
		$title .= "等多个商品";
	}

} elseif ($num > 1) {
	$order = $this->website['class']['db']
		->table('product_order')
		->field('total', 'postage', 'orderid', 'params')
		->where('id in(' . implode(',', $orderid) . ') and uid=? and sta=0', $uid)
		->select();
	if (count($order) !== $num) {
		$json['msg'] = '订单中有不存在或已支付过的订单,请刷新!';
		$json['code'] = 1002;
		$this->json_encode_encrypt($json);
	}
	foreach ($order as $v) {
		if (!$order) {$orderCode = $v['orderid'];}
		if (!$title) {
			$params = json_decode($v['params'], 1);
			$pid = $params[0]['pid'];
			$title = $productTitle($pid);
			if (!$title) {
				$json['msg'] = '有商品已下架或已被删除!';
				$json['code'] = 1004;
				$this->json_encode_encrypt($json);
			}
			$title .= "等多个商品";
		}
		$totals += $v['total'] + $v['total'];
	}
} else {
	$json['msg'] = '订单异常!';
	$json['code'] = 1003;
	$this->json_encode_encrypt($json);
}

$json['status'] = 'ok';
